﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;
using BusinessCommon.Utilities;
using System.Resources;
using System.Reflection;

namespace BusinessCommon.Utilities
{
    public class ErrorProcessor
    {
        static CultureInfo culture = CultureInfo.CurrentCulture;

        /// <summary>
        /// Get error message
        /// </summary>
        /// <param name="ErrorCode">ErrorCode</param>
        /// <returns>Error Message</returns>
        public static String GetErrorMessage(string ErrorCode)
        {
            culture = CultureInfo.CreateSpecificCulture("vi-VN");
            ResourceManager rm = new
                ResourceManager(CommonString.LOCALIZE_FOLDER + CommonString.DOT + CommonString.ERR_MSG_FILE, Assembly.GetExecutingAssembly());

            string message = rm.GetString(ErrorCode, culture);

            if (String.IsNullOrEmpty(message))
            {
                message = ItemDefineIF.ERR_UNKNOW + CommonString.COLON_SP + ErrorCode;
            }
            return message;
        }

        /// <summary>
        /// Get list error message
        /// </summary>
        /// <param name="lstErrorCode">List error code</param>
        /// <returns>List error message</returns>
        public static List<string> GetListErrorMessage(List<string> lstErrorCode)
        {
            List<string> errorList = new List<string>();
            foreach (string errorCode in lstErrorCode)
            {
                errorList.Add(GetErrorMessage(errorCode));
            }
            return errorList;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="errCode"></param>
        /// <param name="param"></param>
        /// <returns></returns>
        public static String CreateMessageError(String errCode, string[] param)
        {
            if (String.IsNullOrEmpty(errCode))
            {
                return null;
            }
            string messageErr = GetErrorMessage(errCode);
            if (param != null && param.Length > 0)
            {
                for (int i = 0; i < param.Length; i++)
                {
                    messageErr = messageErr.Replace("{" + i + "}", param[i]);
                }
            }
            return messageErr;
        }

    }
}
